﻿using Models;
using strange.extensions.context.api;
using strange.extensions.dispatcher.eventdispatcher.api;
using strange.extensions.mediation.impl;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace CommonIoC
{
    public class PersonInfoPanelMediator : Mediator
    {

       

        [Inject]
        public PersonInfoPanelView view { get; set; }
        [Inject(ContextKeys.CONTEXT_DISPATCHER)]//全局的dispatcher
        public IEventDispatcher dispatcher { get; set; }

        public override void OnRegister()
        {
           
           // dispatcher.AddListener(PlayerGameState.IsDoingTalk, onIsDoingTalk);
            dispatcher.AddListener(PersonListPanelEvent.ClickSeeOnePersonInfo , onShowPersonInfoData );

            // dispatcher.AddListener(TopButtonListEvent.ClickBack, onTopButtonListClickBack);

            AddZhuangBeiButtonClickFunc();
            AddBackButtonClickFunc();

        }

        private void onShowPersonInfoData(IEvent payload)
        {
            int playerindex = Convert.ToInt32( payload.data );
           // Debug.Log("传递过来的值为："+playerindex);
            UpdatePlayerInfoData(playerindex);
            view.ShowThisUI();
        }




        void AddZhuangBeiButtonClickFunc()
        {
            if (view.btn_zhuangbei != null)
            {
                view.btn_zhuangbei.onClick.AddListener(delegate ()
                {

                    dispatcher.Dispatch(PersonInfoPanelEvent.ClickZhuangBei );
                    view.CloseThisUI();
                });
            }
        }


        void AddBackButtonClickFunc()
        {
            if (view.btn_back != null)
            {
                view.btn_back.onClick.AddListener(delegate ()
                {

                    dispatcher.Dispatch( PersonInfoPanelEvent.ClickBack );
                    view.CloseThisUI();
                });
            }
        }

        void UpdatePlayerInfoData(int _index )
        {
            //Dictionary<string,GamePerson> allplayer = GameDataManager.Instance.GetPersonDic();

            GamePerson  person = GameDataManager.Instance.GetGamePerson();

           // int pcount = allplayer.Count;
            if( person == null)
            {
                return;
            }

            GamePerson p = person;//allplayer[_index.ToString()];

            //view.personImage.sprite = null; //p.pbase.IconSrc;
            view.person_name.text = p.pbase.PlayerName;
            view.person_lv.text = string.Format("等级：{0}", p.pbase.Lv );
            view.person_exp.text = string.Format("经验值：{0}", p.pbase.Exp);
            view.person_hp.text = string.Format("生命:{0}/{1}",
                            p.pbase.NowHp,  p.pbase.MaxHp );

            int zhuangBeiAttack = CommonHelper.GetZhuangBeiAttack();

            int zhuangBeiFangwei = CommonHelper.GetZhuangBeiFangWei();

            view.person_attack.text = string.Format("攻击：{0}", p.point.Attack + zhuangBeiAttack);

            view.person_fangwei.text = string.Format("防卫：{0}", p.point.FangYu + zhuangBeiFangwei);

            view.person_fightLv.text = string.Format("战斗LV：{0}", p.ability.ZhanDou);
            view.person_fightLv.text = string.Format("修理LV：{0}", p.ability.XiuLi);
            view.person_fightLv.text = string.Format("驾驶LV：{0}", p.ability.JiaShi);

            view.person_qiangdu.text = string.Format("强度：{0}", p.ability.QiangDu);
            view.person_zhili.text = string.Format("智力：{0}", p.ability.ZhiLi);
            view.person_speed.text = string.Format("速度：{0}", p.ability.Speed);
            view.person_tili.text = string.Format("体力：{0}", p.ability.TiLi);



        }

      

        public override void OnRemove()
        {
            //dispatcher.RemoveListener(PlayerStageCommandEvent.canMove, onPlayerStageChangeTocanMove);
            //dispatcher.RemoveListener(PlayerStageCommandEvent.doTalk, onPlayerStageChangeTodoTalk);
            dispatcher.RemoveListener(TopButtonListEvent.ClickPersonList, onShowPersonInfoData);

          
        }


    }
}
